package com.luhan.fmmall.service.impl;

import com.luhan.fmmall.dao.ShoppingCartMapper;
import com.luhan.fmmall.entity.ShoppingCart;
import com.luhan.fmmall.entity.ShoppingCartVO;
import com.luhan.fmmall.service.ShoppingCartService;
import com.luhan.fmmall.vo.ResStatus;
import com.luhan.fmmall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    @Override
    public ResultVO addShoppingCart(ShoppingCart shoppingCart) {
        shoppingCart.setCartTime(sdf.format(new Date()));
        int i = shoppingCartMapper.insert(shoppingCart);
        if (i>0){
            return new ResultVO(ResStatus.OK,"success",null);
        }else {
            return new ResultVO(ResStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVO selectShopCartByUserId(Integer userId) {
        List<ShoppingCartVO> shoppingCartVOS = shoppingCartMapper.selectShopCartByUserId(userId);
        ResultVO resultVO = new ResultVO(ResStatus.OK, "success", shoppingCartVOS);
        return resultVO;
    }

    @Override
    public ResultVO updateCartNumByCartId(int cartId, int cartNum) {
        int i = shoppingCartMapper.updateCartNumByCartId(cartId, cartNum);
        if (i>0){
            ResultVO resultVO = new ResultVO(ResStatus.OK, "update success", null);
            return resultVO;
        }else {
            ResultVO resultVO = new ResultVO(ResStatus.NO, "update fail", null);
            return resultVO;
        }
    }

    @Override
    public ResultVO deleteCartShopByCartId(int cartId) {
        int i = shoppingCartMapper.deleteCartShopByCartId(cartId);
        if (i>0){
            return new ResultVO(ResStatus.OK,"delete success",null);
        }else {
            return new ResultVO(ResStatus.NO,"delete fail",null);
        }
    }

    @Override
    public ResultVO listShopCartByCids(String cids) {
        String[] str = cids.split(",");
        List<Integer> list = new ArrayList<>();
        for (int i=0;i<str.length;i++){
            Integer cid = Integer.parseInt(str[i]);
            list.add(cid);
        }
        List<ShoppingCartVO> shoppingCartVOS = shoppingCartMapper.selectShopCartByCids(list);
        ResultVO resultVO = new ResultVO(ResStatus.OK, "success", shoppingCartVOS);
        return resultVO;
    }
}
